package com.pubinfo.passbook.api.controller.engine;

import com.pubinfo.passbook.common.entity.TEngineComputeChain;
import com.pubinfo.passbook.common.exception.ServiceException;
import com.pubinfo.passbook.common.model.dto.engine.EngineChainParam;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import com.pubinfo.passbook.common.model.vo.base.MyPage;
import com.pubinfo.passbook.common.service.engine.EngineComputeChainService;
import com.pubinfo.passbook.engine.RuleEngineEntranceDefault;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;

/**
 * @author zhouch
 * @date 2021/12/20 16:47
 */
@Api(tags = "计算引擎-计算链管理")
@RestController
@RequestMapping("/engine/chain")
public class EngineChainController {

    @Resource
    EngineComputeChainService chainService;

    @Resource
    RuleEngineEntranceDefault engineEntrance;

    @PostMapping("/page")
    @ApiOperation("分页查询")
    public JsonResult<MyPage<TEngineComputeChain>> page(@RequestBody @Valid EngineChainParam params) {
        return JsonResult.page(chainService.page(params));
    }

    @PostMapping("/save")
    @ApiOperation("保存 (响应时间可能比较长)")
    public JsonResult<Boolean> save(@RequestBody @Valid TEngineComputeChain chainInfo) {
        if (chainInfo.getId() != null) {
            throw new ServiceException("ID字段存在");
        }

        if (chainService.existDistrictID(chainInfo.getDistrictId())) {
            throw new ServiceException("所选择地区已经被其他计算链绑定.");
        }

        // 计算链检查
        engineEntrance.checkComputeChain(chainInfo);
        return JsonResult.success(chainService.save(chainInfo));
    }

    @PostMapping("/update")
    @ApiOperation("更新 (响应时间可能比较长)")
    public JsonResult<Boolean> update(@RequestBody @Valid TEngineComputeChain chainInfo) {
        if (chainInfo.getId() == null) {
            throw new ServiceException("ID字段不存在");
        }

        // 计算链检查
        engineEntrance.checkComputeChain(chainInfo);
        return JsonResult.success(chainService.updateById(chainInfo));
    }

    @GetMapping("/remove/{id}")
    @ApiOperation("删除")
    public JsonResult<Boolean> remove(@PathVariable("id") Integer chainId) {
        return JsonResult.success(chainService.removeById(chainId));
    }

    @GetMapping("/get/{id}")
    @ApiOperation("详情")
    public JsonResult<TEngineComputeChain> get(@PathVariable("id") Integer chainId) {
        return JsonResult.success(chainService.getById(chainId));
    }

    @GetMapping("/common/run_by_id/{id}")
    @ApiOperation("通用计算链执行测试通过ID")
    public JsonResult<BigDecimal> commonRun(@PathVariable("id") Integer id) {
        return JsonResult.success(engineEntrance.executeCommonComputeChainByChainId(id, new BigDecimal(0)));
    }

    @PostMapping("/common/run")
    @ApiOperation("通用计算链执行测试")
    public JsonResult<BigDecimal> commonRun(@RequestBody @Valid TEngineComputeChain chainInfo) {
        return JsonResult.success(engineEntrance.executeCommonComputeChain(chainInfo, new BigDecimal(0)));
    }


}
